﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace UserAccountsManager
{
    public partial class OknoVnosUporabnikov : Form
    {
        private class user
        {
            public string ime { get; set; }
            public string priimek { get; set; }
            public string email { get; set; }
            public string geslo { get; set; }
            public user(string ime, string priimek, string email, string geslo)
            {
                this.ime = ime;
                this.priimek = priimek;
                this.email = email;
                this.geslo = geslo;
            }
        }

        bool zamenjaj = false;

        public string domena = "";

        private Random r = new Random();

        private List<user> users = new List<user>();

        public OknoVnosUporabnikov()
        {
            InitializeComponent();
        }

        private string replaceSumnike(string input)
        {
            string output = input.Replace('š', 's');
            output = output.Replace('č', 'c');
            output = output.Replace('ž', 'z');
            output = output.Replace('Š', 'S');
            output = output.Replace('Č', 'C');
            return output.Replace('Ž', 'Z');
        }

        private void beriDatoteko()
        {
            try
            {
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    TextReader reader = new StreamReader(openFileDialog1.OpenFile(), Encoding.Default);
                    String line = "";
                    richTextBox1.Text = "";
                    String temp = "";
                    String temp1 = "";
                    String temp2 = "";
                    try
                    {
                        line = reader.ReadLine();
                        while (line != null)
                        {
                            temp = line.TrimEnd(' ').TrimStart(' ');
                            temp1 = temp.Split(' ')[0];
                            temp2 = temp.Split(' ')[1];
                            temp1 = char.ToUpper(temp1[0]) + temp1.Substring(1).ToLower();
                            temp2 = char.ToUpper(temp2[0]) + temp2.Substring(1).ToLower();
                            richTextBox1.AppendText(temp1 + " " + temp2 + "\r\n");
                            line = reader.ReadLine();
                        }
                    }
                    catch
                    {
                        reader.Close();
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void generirajUporabnike(bool zamenjaj)
        {
            try
            {
                listView1.Items.Clear();
                users.Clear();
                string ime;
                string priimek;
                string email;
                string geslo;
                foreach (string line in richTextBox1.Lines)
                {
                    string imepriimek = line.TrimEnd(' ');
                    if (imepriimek == "") continue;
                    if (zamenjaj)
                    {
                        ime = imepriimek.Split(' ')[1];
                        priimek = imepriimek.Split(' ')[0];
                    }
                    else
                    {
                        ime = imepriimek.Split(' ')[0];
                        priimek = imepriimek.Split(' ')[1];
                    }
                    //ime = imepriimek.Split(' ')[0];
                    //priimek = imepriimek.Split(' ')[1];
                    email = replaceSumnike(ime.ToLower() + "." + priimek.ToLower() + "@" + domena);
                    if (ime.Length > 4)
                    {
                        geslo = ime.Remove(4);
                    }
                    else
                    {
                        geslo = ime;
                    }
                    while (geslo.Length < 8)
                    {
                        geslo += (r.Next() % 10).ToString();
                    }
                    geslo = replaceSumnike(geslo);
                    listView1.Items.Add(email).SubItems.AddRange(new string[3] { ime, priimek, geslo });
                    users.Add(new user(ime, priimek, email, geslo));
                }
                foreach (ColumnHeader col in listView1.Columns)
                {
                    col.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                }
            }
            catch
            {
            }
        }

        private void zamenjajImePriimek()
        {
            //foreach (ListViewItem item in listView1.Items)
            //{
            //    String temp = item.SubItems[1].Text;
            //    item.SubItems[1].Text = item.SubItems[2].Text;
            //    item.SubItems[2].Text = temp;
            //}
            zamenjaj = !zamenjaj;
            generirajUporabnike(zamenjaj);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            beriDatoteko();
        }

        private void odpriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            beriDatoteko();
        }

        private void zapriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void generirajToolStripMenuItem_Click(object sender, EventArgs e)
        {
            generirajUporabnike(zamenjaj);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            generirajUporabnike(zamenjaj);
        }

        private void zamenjajImeInPriimekToolStripMenuItem_Click(object sender, EventArgs e)
        {
            zamenjajImePriimek();
        }

        private void googleCSVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            izvoziGoogleCSV();
        }

        private void izvoziGoogleCSV()
        {
            try
            {
                saveFileDialog1.FileName = openFileDialog1.SafeFileName.Split('.')[0] + "-google-" + domena + ".csv";
                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Stream stream = saveFileDialog1.OpenFile();
                    Encoding enc = Encoding.UTF8;
                    TextWriter writer = new StreamWriter(stream, enc);
                    writer.Write("email address,first name,last name,password\n");
                    foreach (user usr in users)
                    {
                        writer.Write(usr.email + "," + usr.ime + "," + usr.priimek + "," + usr.geslo + "\n");
                    }
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void moodleCSVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            izvoziMoodleCSV();
        }

        private void izvoziMoodleCSV()
        {
            try
            {
                saveFileDialog1.FileName = openFileDialog1.SafeFileName.Split('.')[0] + "-moodle-" + domena + ".csv";
                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Stream stream = saveFileDialog1.OpenFile();
                    Encoding enc = Encoding.UTF8;
                    TextWriter writer = new StreamWriter(stream, enc);
                    writer.Write("username, password, firstname, lastname, email\n");
                    foreach (user usr in users)
                    {
                        writer.Write(usr.email.Split('@')[0] + ", " + usr.geslo + ", " + usr.ime + ", " + usr.priimek + ", " + usr.email + "\n");
                    }
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void natisniToolStripMenuItem_Click(object sender, EventArgs e)
        {
            natisniRezultate();
        }

        private void natisniRezultate()
        {
            if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                printDocument1.DocumentName = "Uporabniki";
                printDocument1.Print();
            }
        }

        private int printedLines;
        private int linesToPrint;

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Font printFont = new Font("Arial", 10);
            float yPos = 0f;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            while (count < linesPerPage && linesToPrint > printedLines)
            {
                if (count == 0) line = "Ime Priimek Email Geslo";
                else
                {
                    line = listView1.Items[printedLines].SubItems[1].Text + " " + listView1.Items[printedLines].SubItems[2].Text + " " + listView1.Items[printedLines].SubItems[0].Text + " " + listView1.Items[printedLines].SubItems[3].Text;
                    printedLines++;
                }
                yPos = topMargin + count * printFont.GetHeight(e.Graphics);
                e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                count++;
            }
            if (linesToPrint > printedLines)
            {
                e.HasMorePages = true;
            }
        }

        private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            printedLines = 0;
            linesToPrint = listView1.Items.Count;
        }

        private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

        }
    }
}
